message = "What's up?"
print(message)
What's up?
x = 5
y = 10
z = x + y
print(f'The answer is {z}.')
The answer is 15.
L = list(range(1,10))
s = 0
for i in range(0, 9):
s = s + L[i]
print(s)
# or just sum(L)
45
s = 0
for i in range(1,21):
if i % 2 == 0: #Missing :
s = s + i
print(s)
110
n = 0
while n <= 21:
n += 1
if n % 4 == 1:
break # need the spacing!
print(n)
1
s = 0
for i in range(1,11):
s = s + i**i
print(s)
10405071317
s = sum([i**i for i in range(1,11)])
print(s)
10405071317
x = 1
while (x >= 0) and (x <= 19):
for i in range(2, 4):
x = i*x
print(x)
36
x = 1
while (x >= 0) and (x <= 19):
for i in range(2, 4):
x = i*x
print(x)
6 36
#Part c:
x = 1
while (x >= 0) and (x <= 19):
for i in range(2,4):
x = i*x
print(x)
2 6 12 36
W = ['m', 'a', 't', 'h']
i = 0
while W[i] != 'a' or W[i] != 't':
i += 1
W.append(W[i])
print(W)
# It runs forever unless you stop the kernel
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-11-39385bac0c7c> in <module>() 3 while W[i] != 'a' or W[i] != 't': 4 i += 1 ----> 5 W.append(W[i]) 6 print(W) 7 # It runs forever unless you stop the kernel KeyboardInterrupt:
W = ['m', 'a', 't', 'h']
i = 0
while W[i] == 'a' or W[i] == 't':
i += 1
W.append(W[i])
print(W)
# This will print ['m', 'a', 't', 'h'] since it will never enter the while loop
['m', 'a', 't', 'h']
W = ['m', 'a', 't', 'h']
i = 0
while W[i] == 'm' or W[i] == 'a':
i += 1
W.append(W[i])
print(W)
# Here the first two intances of the while loop are true so we carry out the
# instructions.
# Note that the variable i changes before we add letters to the string. Hence
# we add 'a' and 't'.
['m', 'a', 't', 'h', 'a', 't']
def sumavefunc(L):
s = sum(L)
a = sum(L) / len(L)
return [s,a]
Make a file called sumavefunc.py
that has this function in it.
Then in a another file have the scripts:
from sumavefunc import sumavefunc
Then you can call this function from this separate file as long as these files are in the same directory.
def truckprice(model, wd, color, accessories):
if model == 101:
price = 10000
elif model == 201:
price = 12000
if wd == 4:
price = price + 2000
if color == 'metallic':
price = price + 500
elif color == 'race stripe':
price = price + 700
for x in accessories:
price = price + 200
return price
truckprice(101, 4, 'red', ['heated seats', 'premium sound'])
12400
truckprice(201, 2, 'race stripe', ['sun roof'])
12900
truckprice(301, 4, 'blue', ['self drive', 'self wash'])
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-18-4e5f4b6d80ce> in <module>() ----> 1 truckprice(301, 4, 'blue', ['self drive', 'self wash']) <ipython-input-15-2b9cc81785d8> in truckprice(model, wd, color, accessories) 5 price = 12000 6 if wd == 4: ----> 7 price = price + 2000 8 if color == 'metallic': 9 price = price + 500 UnboundLocalError: local variable 'price' referenced before assignment
def fibsq(f1, f0=1): # The == should be = and Missing the : and the default argument should follow the non-default argument.
F = [f0, f1]
sumsq = lambda x, y : x**2 + y**2 # == should be : and ^ should be **
lastnumber=0
while lastnumber < 100: # delete for
lastnumber = sumsq(F[-1], F[-2])
F.append(lastnumber)
return F
fibsq(3)
[1, 3, 10, 109]
fibsq(-2, 4)
[4, -2, 20, 404]
D = {'Alice': 45, 'Bob': 22, 'Carl': 11, 'Diana': 57}
L = []
for x in D.keys():
L.append(x)
S = 0
for y in D.values():
S += y